﻿using AutoMapper;
using B.S.ProcessEgine.API.Applocations.Commands.FormNodes;
using B.S.ProcessEgine.ErrorCode;
using B.S.ProcessEgine.Infractructure.Implements;
using B.S.ProcessEgine.Infractructure.Interfaces;
using MediatR;
using System.Transactions;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.FormNodes
{
    public class DeleteFormNodeCommandHandler : IRequestHandler<DeleteFormNodeCommand, ApiResult>
    {

        private readonly IFormNodeRepository formNode;
        private readonly IMapper mapper;
        private readonly IFormInfoRepository formInfo;
        private readonly IFormTypeRepository formType;

        public DeleteFormNodeCommandHandler(IFormNodeRepository formNode, IMapper mapper, IFormInfoRepository formInfo, IFormTypeRepository formType)
        {
            this.formNode = formNode;
            this.mapper = mapper;
            this.formInfo = formInfo;
            this.formType = formType;
        }
        public async Task<ApiResult> Handle(DeleteFormNodeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var formNodes = await formNode.GetAsync(x=>x.NodeId==request.NodeId);
                if (formNode == null)
                {
                    return ApiResult.Fail("节点不存在", StateCodeEnum.Exception);
                }

                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        //先删除当前节点
                        formNodes.IsDel = true;
                        var res = await formNode.UpdateAsync(formNodes);

                        if (res == 0)
                        {
                            return ApiResult.Fail("删除节点失败", StateCodeEnum.Exception);
                        }
                        //获取被删除节点的上下级
                        var prev = await formNode.GetAsync(x => x.NodeNo == formNodes.PrevNodeNo && x.FormId == formNodes.FormId && !x.IsDel);
                        var next = await formNode.GetAsync(x => x.NodeNo == formNodes.NextNodeNo && x.FormId == formNodes.FormId && !x.IsDel);
                        //如果上级不为空 则把上级编号的下级编号修改为  被删除节点的下级编号
                        if (prev != null)
                        {
                            prev.NextNodeNo = next?.NodeNo??string.Empty;//next?(这里的问号是只拿next不为空时的值).NodeNo??(这里则是next为空时取后面string.Empty的值)string.Empty
                            await formNode.UpdateAsync(prev);
                        }
                        //如果下级不为空 则把下级编号的上级编号修改为  被删除节点的上级编号
                        if (next != null)
                        {
                            next.PrevNodeNo = prev?.NodeNo??string.Empty;
                            await formNode.UpdateAsync(next);
                        }

                        //提交事务
                        scope.Complete();

                        return ApiResult.Success(StateCodeEnum.Ok);
                    }
                    catch (Exception)
                    {
                        scope.Dispose();
                        return ApiResult.Fail("删除表单节点异常", StateCodeEnum.Exception);
                    }
                }

            }
            catch (Exception)
            {

                return ApiResult.Fail("删除表单节点异常", StateCodeEnum.Exception);
                throw;
            }
        }
    }
}
